package com.fosun.spr.plugin.uuc.api.uucRole.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.uuc.api.uucRole.dao.UucRoleDao;
import com.fosun.spr.plugin.uuc.api.uucRole.service.UucRoleService;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.DeleteSpModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.DeleteSrModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.GetSpModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.GetSrModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.ListSpModel;
import com.fosun.spr.plugin.uuc.api.uucRole.service.model.ListSrModel;
import com.fosun.spr.plugin.uuc.helper.mybatis.dao.SprUucRoleMapper;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucRole;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucRole;
import com.fosun.spr.plugin.uuc.helper.mybatis.model.SprUucRoleExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class UucRoleServiceImpl extends CoBaseService implements
		UucRoleService {
	@Autowired 
	private UucRoleDao uucRoleDao;
	@Autowired
	private SprUucRoleMapper sprUucRoleMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(uucRoleDao.list(spModel));
		srModel.setTotalCount(uucRoleDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		SprUucRole db = sprUucRoleMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"权限管理");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprUucRole db = sprUucRoleMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		sprUucRoleMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getRoleName(),"角色名称");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		SprUucRoleExample eRoleName = new SprUucRoleExample();
		SprUucRoleExample.Criteria cRoleName = eRoleName.createCriteria();
		cRoleName.andRoleNameEqualTo(spModel.getRoleName());
		cRoleName.andIsDeletedEqualTo(IS_DELETED_N);
		List<SprUucRole> rowsRoleName = sprUucRoleMapper.selectByExample(eRoleName);		
		
		if(StringUtils.isBlank(spModel.getId())){
			if(rowsRoleName.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getRoleName()+" 已经存在");
			}		
			
			SprUucRole db = new SprUucRole();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			db.setId(getUuid());
			sprUucRoleMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsRoleName.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getRoleName()+" 已经存在");
			}			
			if(rowsRoleName.size()==1 && !rowsRoleName.get(0).getRoleName().equals(spModel.getRoleName())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getRoleName()+" 已经存在");
			}	
			
			SprUucRole db = sprUucRoleMapper.selectByPrimaryKey(spModel.getId());
			db.setRoleName(spModel.getRoleName());
			db.setMemo(spModel.getMemo());
			sprUucRoleMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
